package com.webchat.ugc.service.mall;


import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.enums.mall.MallAccountTypeEnum;
import com.webchat.domain.vo.response.mall.MallShopBaseVO;
import com.webchat.domain.vo.response.mall.im.MallChatAccountBaseVO;
import com.webchat.rmi.pgc.mall.MallShopClient;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class MallShopService {

    @Autowired
    private MallShopClient mallShopClient;

    /**
     * 查询单个店铺信息
     *
     * @param shopId
     * @return
     */
    public MallShopBaseVO shopInfo(Long shopId) {

        APIResponseBean<MallShopBaseVO> response = mallShopClient.shopInfo(shopId);
        if (APIResponseBeanUtil.isOk(response)) {
            return response.getData();
        }
        // TODO 预警、兜底
        return null;
    }

    /**
     * 获取店铺管理员
     *
     * @param shopId
     * @return
     */
    public String getShopManager(Long shopId) {
        MallShopBaseVO shop = shopInfo(shopId);
        return shop != null ? shop.getCreateBy() : null;
    }

    /**
     * 查询账号管理的店铺ID
     *
     * @param account
     * @return
     */
    public Long getManageShopId(String account) {
        APIResponseBean<Long> response = mallShopClient.manageShopId(account);
        if (APIResponseBeanUtil.isOk(response)) {
            return response.getData();
        }
        // TODO 预警、兜底
        return null;
    }

    public boolean isShopManager(String account) {
        return getManageShopId(account) != null;
    }

    /**
     * 批量获取店铺信息
     *
     * @param shopIds
     * @return
     */
    public List<MallShopBaseVO> batchGetShopList(List<Long> shopIds) {

        APIResponseBean<List<MallShopBaseVO>> response = mallShopClient.batchGetShopListInfo(shopIds);
        if (APIResponseBeanUtil.isOk(response)) {
            return response.getData();
        }
        // TODO
        return null;
    }

    public List<MallChatAccountBaseVO> batchGetMallChatAccountList(List<Long> shopIds) {
        List<MallShopBaseVO> shops = this.batchGetShopList(shopIds);
        if (CollectionUtils.isEmpty(shops)) {
            return Collections.emptyList();
        }
        return shops.stream().map(s -> {
            MallChatAccountBaseVO mallChatAccountBaseVO = new MallChatAccountBaseVO();
            mallChatAccountBaseVO.setAccount(String.valueOf(s.getId()));
            mallChatAccountBaseVO.setAccountName(s.getName());
            mallChatAccountBaseVO.setAccountLogo(s.getLogo());
            mallChatAccountBaseVO.setAccountType(MallAccountTypeEnum.MERCHANT.getType());
            return mallChatAccountBaseVO;
        }).toList();
    }
}
